﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_getSamplePointsAndQuality : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        string imageId = Request.QueryString[0].ToString().Trim();
        string rootId = Request.QueryString[1].ToString().Trim();//选择的省或直辖市编号
        string distId = Request.QueryString[2].ToString().Trim();//选择的二级城市编号

        string result = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        string SP_FILE_NAME = "";
        string DistrictId = "";

        string[] ImageIdArray = imageId.Split('_');
        string samplingDateString = ImageIdArray[2] + "-" + ImageIdArray[3] + "-" + ImageIdArray[4];//选中的影像的日期

        string queryString = "select SP_FILE_NAME,DISTID from RS.DISTRICT_IMAGE_SP_REF where IMAGEID = '" + imageId + "'";
        DataSet ds = OracleDataBase.Query(queryString);
        DataTable dt = ds.Tables[0];
        if (dt.Rows.Count == 1)
        {
            SP_FILE_NAME = dt.Rows[0][0].ToString();
            DistrictId = dt.Rows[0][1].ToString();
        }
        //string IMAGE_NAME = dt.Rows[0][2].ToString();
        dt.Dispose();
        ds.Dispose();

        if (Convert.ToInt32(DistrictId) > 50)//判断该行政区是否是省级
        {
            queryString = "select POINTNAME,JIN,WEI,POINTID from rs." + SP_FILE_NAME + " where distid = '" + DistrictId + "'";
        }
        else
        {
            queryString = "select POINTNAME,JIN,WEI,POINTID from rs." + SP_FILE_NAME + " where rootid = '" + DistrictId + "'";
        }
        ds = OracleDataBase.Query(queryString);
        dt = ds.Tables[0];//样点文件集合

        queryString = "select POINTNAME,NCL,CND,CHL_A,DEN_CHLA,CHL_AB,DEN_CHL,POINTID FROM RS.SP_AGRI_PARAM WHERE SAMPDATE = to_date('" + samplingDateString + "','yyyy-mm-dd')";//'2005-1-1','yyyy-mm-dd'.ToShortDateString() + "'";
        DataSet ds1 = OracleDataBase.Query(queryString);
        DataTable dt1 = ds1.Tables[0];//农学参数集合

        //查询得到品质数据集合
        //queryString = "select * from rs.sp_quality_param";
        //DataSet q_ds = OracleDataBase.Query(queryString);
        //DataTable quality_dt = q_ds.Tables[0];


        //将样点文件输出到XML文档
        result += "<SamplePoints>" + "\n";

        if (dt.Rows.Count > 0 && dt.Rows.Count == dt1.Rows.Count)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    if (dt1.Rows[i][0].ToString() == dt.Rows[k][0].ToString())
                    {
                        result += "<Point name = '" + dt.Rows[k][0].ToString() + "' Lon = '" + dt.Rows[k][1].ToString() + "' Lat = '" + dt.Rows[k][2].ToString() + "'";

                    }
                }
                for (int j = 0; j < dt1.Rows.Count; j++)
                {
                    if (dt1.Rows[j][0].ToString().ToUpper() == dt.Rows[i][0].ToString().ToUpper())
                    {
                        result += " NCL = '" + dt1.Rows[i][1].ToString() + "' CND = '" + dt1.Rows[i][2].ToString() + "' CHL_A = '" + dt1.Rows[i][3].ToString() + "' DEN_CHLA = '" + dt1.Rows[i][4].ToString() + "' CHL_AB = '" + dt1.Rows[i][5].ToString() + "' DEN_CHL = '" + dt1.Rows[i][6].ToString()  + "'/>" + "\n";
                        break;
                    }
                }
                //将品质数据添加到节点的属性
                //if (quality_dt.Rows.Count > 0)
                //{
                //    foreach (DataRow row in quality_dt.Rows)                        
                //    {
                //        //string a= row["POINTID"].ToString();
                //        //string b = dt.Rows[i][3].ToString();
                //        if (row["POINTID"].ToString() == dt.Rows[i][3].ToString())
                //        {
                //            result += " CRUDEPP ='" + row["CRUDEPP"].ToString() + "' WETGP ='" + row["WETGP"].ToString() + "' SETTIME ='" + row["SETTIME"].ToString() + "' NCONTENT ='" + row["NCONTENT"].ToString()+"'/>"+"\n";
                //        }
                        
                //    }
                //}
                //else
                //{
                //    result += "'/>" + "\n";
                //}
            }
        }
        result += "</SamplePoints>";
        Response.Write(result);
    }
}
